package com.zmovie.front.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zmovie.common.response.CommonReturnType;
import com.zmovie.common.utils.FileUtils;
import com.zmovie.framework.domain.*;
import com.zmovie.framework.domain.dto.ActorGroupDTO;
import com.zmovie.framework.domain.dto.ActorGroupItemDto;
import com.zmovie.framework.service.*;
import com.zmovie.front.constant.Constants;
import com.zmovie.front.controller.vo.CinemaVO;
import com.zmovie.front.controller.vo.MovieVO;
import com.zmovie.front.controller.vo.SessionVO;
import com.zmovie.front.util.MoviePathUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.CallNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec com.zmovie.front.controller
 * @since 2023-12-21 20:03
 */
@Controller
@RequestMapping("/chooseSeat")
@Api(tags = "选座购票")
@Slf4j
public class ChooseSeatController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private CinemaService cinemaService;
    @Autowired
    private SessionService sessionService;
    @Autowired
    private MovieToTypeService movieToTypeService;
    @Autowired
    private CinemaAreaService cinemaAreaService;
    @Autowired
    private MovieRuntimeService movieRuntimeService;
    @Autowired
    private HallService hallService;
    @GetMapping
    @ApiOperation("选座页面")
    public String index(Model model, Integer movieId, Integer cinemaId, Integer sessionId){
        //电影信息
        Movie movie = movieService.getById(movieId);
        model.addAttribute(Constants.MOVIE_KEY, convert(movie));
        //影院信息
        Cinema cinema = cinemaService.getById(cinemaId);
        model.addAttribute(Constants.CINEMA_KEY, cinema);
        //场次信息
        Session session = sessionService.getById(sessionId);
        model.addAttribute(Constants.SESSION_KEY, convert(session));
        //座位信息
        List<int[]> seats = JSON.parseArray(session.getSeats(), int[].class);
        //座位信息
        model.addAttribute("row", seats.size());
        model.addAttribute("col", seats.get(0).length);
        model.addAttribute(Constants.SEATS_KEY, seats);
        return "choose_seat";
    }
    @PutMapping
    @ResponseBody
    @ApiOperation("提交订单")
    public Object submit(String seats, Integer sessionId, HttpSession session){
        Session session1 = sessionService.getById(sessionId);
        List<int[]> selectSeats = JSON.parseArray(seats, int[].class);
        session.setAttribute(Constants.SESSION_USER_SELECTED_SEATS_KEY, selectSeats);
        session.setAttribute(Constants.SESSION_USER_SELECTED_SESSION_ID_KEY, sessionId);
        session.setAttribute("totalPrice", session1.getPrice().multiply(new BigDecimal(selectSeats.size())));
        return CommonReturnType.success("提交成功!");
    }
    private MovieVO convert(Movie movie){
        //处理文件路径
        MovieVO movieVO = MoviePathUtil.dealMovieFilePath(movie);
        //电影类型
        List<MovieType> types = movieToTypeService.listTypeByMovieId(movie.getId());
        movieVO.setTypes(types);
        return movieVO;
    }
    private CinemaVO convert(Cinema cinema) {
        CinemaVO cinemaVO = new CinemaVO();
        BeanUtils.copyProperties(cinema, cinemaVO);
        //影院地址
        CinemaArea area = cinemaAreaService.getById(cinema.getAreaId());
        cinemaVO.setCinemaArea(area);
        return cinemaVO;
    }
    private SessionVO convert(Session session){
        SessionVO sessionVO = new SessionVO();
        BeanUtils.copyProperties(session, sessionVO);
        //播放时段
        MovieRuntime runtime = movieRuntimeService.getById(sessionVO.getMovieRuntimeId());
        sessionVO.setMovieRuntime(runtime);
        //影厅
        Hall hall = hallService.getById(session.getHallId());
        sessionVO.setHall(hall);
        return sessionVO;
    }
}
